package com.computer.network.controller;

import com.computer.network.po.SysRange;
import com.computer.network.po.SysUser;
import com.computer.network.service.AuthorityService;
import com.computer.network.service.UserService;
import com.computer.network.vo.ResponseVO;
import com.computer.network.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/user")
@CrossOrigin
public class UserController {
    @Autowired
    UserService userService;

    @Resource
    private AuthorityService authorityService;

    @PostMapping("/register")
    public ResponseVO addUser(@RequestBody UserVO userVO){
        return userService.addUser(userVO);
    }

    @PostMapping("/login")
    public ResponseVO login(@RequestBody UserVO userVO){
        System.out.println(userVO);
        return userService.login(userVO);
    }

    /**
     * 添加用户方法
     * @param sysUser
     * @return
     */
    @PostMapping("/addUser")
    public ResponseVO addUser(@RequestBody SysUser sysUser){
        boolean b = authorityService.addUser(sysUser);
        if (b) {
            return ResponseVO.buildSuccess();
        }else {
            return ResponseVO.buildFailure("添加失败");
        }
    }

    /**
     * 用户删除方法
     * @param user
     * @return
     */
    @DeleteMapping("delUser")
    public ResponseVO delUser(@RequestBody SysUser user){
        boolean b = authorityService.removeUser(user);
        if (b) {
            return ResponseVO.buildSuccess();
        }else {
            return ResponseVO.buildFailure("删除失败");
        }
    }

    /**
     * 用户更新方法
     * @param sysUser
     * @return
     */
    @PutMapping("updateUser")
    public ResponseVO updateUser(@RequestBody SysUser sysUser){
        boolean b = authorityService.updateUser(sysUser);
        if (b) {
            return ResponseVO.buildSuccess();
        }else {
            return ResponseVO.buildFailure("更新失败");
        }
    }

    /**
     * 添加权限
     * @param sysRange
     * @return
     */
    @PostMapping("addRange")
    public ResponseVO addRange(@RequestBody SysRange sysRange){
        boolean b = authorityService.addRange(sysRange);
        if (b) {
            return ResponseVO.buildSuccess();
        }else {
            return ResponseVO.buildFailure("权限添加失败");
        }
    }

    /**
     * 删除权限
     * @param sysRange
     * @return
     */
    @DeleteMapping("delRange")
    public ResponseVO delRange(@RequestBody SysRange sysRange){
        boolean b = authorityService.removeRange(sysRange);
        if (b) {
            return ResponseVO.buildSuccess();
        }else {
            return ResponseVO.buildFailure("权限删除失败");
        }
    }

    /**
     * 查询所有权限
     * @return
     */
    @GetMapping("getList")
    public ResponseVO getList(){
        List<SysRange> sysRanges = authorityService.selectRange();
        return ResponseVO.buildSuccess(sysRanges);
    }
}
